package demo.zyy.com.dreamstudio.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import demo.zyy.com.dreamstudio.R;
import demo.zyy.com.dreamstudio.bean.Drink;

/**
 * Created by Administrator on 2017/11/1.
 */

public class CategoryFgAdapter extends BaseAdapter {
    private ArrayList<Drink> datalist;
    private static HashMap<Integer, Boolean> isSelected;
    //上下文
    private Context context;
    //用来导入布局
    private LayoutInflater inflater = null;
    private boolean isShow = false;

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        CategoryFgAdapter.isSelected = isSelected;
    }

    public CategoryFgAdapter(ArrayList<Drink> datalist, Context context, boolean isShow) {
        this.datalist = datalist;
        this.context = context;
        this.isShow = isShow;
        inflater=LayoutInflater.from(context);
        isSelected=new HashMap<Integer, Boolean>();

        //初始化数据
        initDatas();
    }

    //初始化isSelected的数据
    private void initDatas() {
        for (int i = 0; i < datalist.size(); i++) {
            getIsSelected().put(i, false);
        }

    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CategoryFgHolder holder = null;
        if (convertView == null) {
            //获取CateverView对象
            holder = new CategoryFgHolder();
            //导入布局并赋值给convertview
            convertView = inflater.inflate(R.layout.categort_fragment_item, null);
            holder.categort_itemimage = (ImageView) convertView.findViewById(R.id.categort_itemimage);
            holder.categort_itemcheckbok = (CheckBox) convertView.findViewById(R.id.categort_itemcheckbok);
            holder.categort_name = (TextView) convertView.findViewById(R.id.categort_name);
            holder.categort_price = (TextView) convertView.findViewById(R.id.categort_price);
            //为view设置标签
            convertView.setTag(holder);
        } else {
            holder = (CategoryFgHolder) convertView.getTag();
        }
        //获取数据
        Drink food = datalist.get(position);
        if (isShow) {
            holder.categort_itemcheckbok.setVisibility(View.VISIBLE);
        } else {
            holder.categort_itemcheckbok.setVisibility(View.GONE);
        }
        holder.categort_itemimage.setImageResource(food.getImage());
        holder.categort_name.setText(food.getName());
        holder.categort_price.setText(food.getPrice());
        holder.categort_itemcheckbok.setChecked(getIsSelected().get(position));
        return convertView;
    }

   public  class CategoryFgHolder {
       public CheckBox categort_itemcheckbok;
       public  TextView categort_name, categort_price;
       public   ImageView categort_itemimage;
   }


}
